﻿using System;
using Framework.MVC;
using Identity.Model;
using Account.Model;
using Account.Model.Behavior;
using Framework.MVVM.Model;
using Identity.IService;
using Identity.Service;
using Account.IService;
using Account.Service;

namespace FrontCenter.Models
{
    [ServiceAttribute(typeof(IVerifyCodeService), typeof(VerifyCodeService), "Instance")]
    [ServiceAttribute(typeof(IAccountService), typeof(AccountService), "Instance")]
    [ServiceAttribute(typeof(IIdentityService), typeof(IdentityService), "Instance")]
    public class ModifyAccountModel : AsyncModelBase, IValidateVerifyCode,IDeleteVerifyCode, IValidateIdentity, IModifyAccount
    {
        #region Filed

        private const string m_verifyvalue_complete = "naccount4";

        private const string m_resource = "Normal";

        private const string m_user_resource = "modifypassword";

        private const string m_result = "result";

        private const string m_error = "error";

        #endregion Filed

        #region Property

        public string Resource { get; set; }

        #endregion Property

        #region AsyncModelBase

        protected override void OnInit(Type type)
        {
            OnBeginRequest();
            this.ValidateVerifyCode(m_resource, string.Empty, string.Empty);
        }

        #endregion AsyncModelBase

        #region IValidateVerifyCode

        public string Source { get; set; }

        public string VerifyCode { get; set; }

        public string VerifyValue { get; set; }

        public bool IsOnce { get; set; }

        public void OnValidateVerifyCodeComplete(string key, bool result)
        {
            this.DeleteVerifyCode(m_resource, string.Empty, string.Empty);
        }

        public void OnValidateVerifyCodeError(string key)
        {
            OnComplete(m_error,"verifycode");
            OnEndRequest();
        }

        #endregion IValidateVerifyCode

        #region IDeleteVerifyCode

        public void OnDeleteVerifyCodeComplete(string key, bool result)
        {
            if (!string.Equals(Resource, m_user_resource) && string.IsNullOrWhiteSpace(OldAccountPassword))
            {
                OnComplete(m_error, "resource");
                OnEndRequest();
                return;
            }
            IsOnce = string.Equals(Resource, m_user_resource);
            this.ValidateIdentity(Resource, string.Empty, string.Empty, string.Empty);
        }

        public void OnDeleteVerifyCodeError(string key)
        {
            if (!string.Equals(Resource, m_user_resource) && string.IsNullOrWhiteSpace(OldAccountPassword))
            {
                OnComplete(m_error, "resource");
                OnEndRequest();
                return;
            }
            IsOnce = string.Equals(Resource, m_user_resource);
            this.ValidateIdentity(Resource, string.Empty, string.Empty, string.Empty);
        }

        #endregion IDeleteVerifyCode

        #region IValidateIdentity

        public string IdentityCode { get; set; }

        public void OnValidateIdentityComplete(string key, Identity.IService.Entry.IdentityEntry entry)
        {
            AccountCode = entry.value;
            this.ModifyAccount(AccountResource,string.Empty, string.Empty);
        }

        public void OnValidateIdentityError(string key)
        {
            OnComplete(m_error, "identity");
            OnEndRequest();
        }

        #endregion IValidateIdentity

        #region IModifyAccount

        public string AccountResource { get; set; }

        public string AccountCode { get; set; }

        public string AccountName { get; set; }

        public string AccountPassword { get; set; }

        public string OldAccountPassword { get; set; }

        public void OnModifyAccountComplete(string key, bool result)
        {
            OnComplete(m_result, result);
            OnEndRequest();
        }

        public void OnModifyAccountError(string key)
        {
            OnComplete(m_error, "account");
            OnEndRequest();
        }

        #endregion IModifyAccount
    }
}